/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.teotigraphix.dx.patterns.layer
{

import flash.events.Event;

import mx.styles.IStyleClient;

import org.teotigraphix.dx.event.SkinLayerEvent;
import org.teotigraphix.dx.interfaces.IComponentAware;
import org.teotigraphix.dx.interfaces.IComponentLayerClient;
import org.teotigraphix.dx.interfaces.IComponentSkin;
import org.teotigraphix.dx.interfaces.ISkinLayer;
import org.teotigraphix.dx.patterns.manager.StyleBindingManager;

/**
 * The default <code>ISkinLayer</code> implementation of an 
 * <code>ISkinLayerAware</code> Component.
 * 
 * @copyright Teoti Graphix, LLC
 * @author Michael Schmalle
 * @date 07-07-09
 */
public class SkinLayer 
	   extends ComponentLayer 
	   implements ISkinLayer
{
	include "../../core/Version.as"
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */	
	public function SkinLayer()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  ISkinLayer API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.ISkinLayer#registerSkin()
	 */
	public function registerSkin(api:Class, skin:IComponentSkin):void
	{
		super.registerProxy(api, skin);
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.ISkinLayer#removeSkin()
	 */
	public function removeSkin(api:Class):IComponentSkin
	{
		return super.removeProxy(api) as IComponentSkin;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.ISkinLayer#hasSkin()
	 */
	public function hasSkin(api:Class):Boolean
	{
		return super.hasProxy(api);
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.ISkinLayer#getSkin()
	 */
	public function getSkin(api:Class):IComponentSkin
	{
		return super.getProxy(api) as IComponentSkin;
	}
	
	/**
	 * @copy org.teotigraphix.dx.interfaces.ISkinLayer#getSkins()
	 */
	public function getSkins():Array
	{
		var proxies:Array = [];
		var items:Array = getAllProxies();
		var len:int = items.length;
		for (var i:int = 0; i < len; i++)
		{
			proxies.push(items[i]);
		}
		return proxies;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	override protected function commitComponent():void
	{
		super.commitComponent();
		
		var proxies:Array = getAllProxies();
		var len:int = proxies.length;
		for (var i:int = 0; i < len; i++)
		{
			var proxy:IComponentAware = proxies[i];
			if (component)
			{
				StyleBindingManager.initialize(this, IStyleClient(component));
				
				// don't set again if already component
				// lock in component handlers and aliases, if skin is not
				// created on component, parts and skin handlers will not
				// be added until the skin is added to the Component's display
				if (proxy.component !== component)
					proxy.component = component;				
			}
			else
			{
				// clear out all listeners and refs from the behavior that
				// are connected to the Component
				proxy.component = null;
			}
		}
	}
	
	/**
	 * @uses org.teotigraphix.dx.event.SkinLayerEvent
	 */
	override protected function createLayerEvent(
		type:String, load:Boolean = false):Event
	{
		return new SkinLayerEvent(type);
	}
	
	/**
	 * @event org.teotigraphix.dx.event.SkinLayerEvent.SKIN_ADD
	 * 
	 * @uses org.teotigraphix.tka.core.ToolKitLayer#createLayerEvent()
	 */
	override protected function dispatchAddEvent(api:Class, proxy:IComponentLayerClient):void
	{
		var e:SkinLayerEvent = 
			createLayerEvent(SkinLayerEvent.SKIN_ADD) as SkinLayerEvent;
		
		e.api = api;
		e.skin = proxy as IComponentSkin;
		e.component = component;
		
		dispatchEvent(e);
		// TODO see if this is correct
		if (component)
			component.dispatchEvent(e);
	}
	
	/**
	 * @event org.teotigraphix.dx.event.SkinLayerEvent.SKIN_REMOVE
	 * 
	 * @uses org.teotigraphix.dx.core.ToolKitLayer#createLayerEvent()
	 */
	override protected function dispatchRemoveEvent(api:Class, proxy:IComponentLayerClient):void
	{
		var e:SkinLayerEvent = 
			createLayerEvent(SkinLayerEvent.SKIN_REMOVE) as SkinLayerEvent;
		
		e.api = api;
		e.skin = proxy as IComponentSkin;
		e.component = component;
		
		dispatchEvent(e);
		// TODO see if this is correct
		if (component)
			component.dispatchEvent(e);
	}	
}
}